package com.xixi.core.rpc.serializer;

import java.io.*;

/**
 * @author: zxh
 * @date: 2024/8/21 10:59
 * @description:
 */
public class CustomJdkSerializer implements CustomSerializer {
	@Override
	public <T> byte[] serialize(T object) throws IOException {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
		objectOutputStream.writeObject(object);
		objectOutputStream.close();
		return outputStream.toByteArray();
	}

	@Override
	public <T> T deserialize(byte[] bytes, Class<T> type) throws IOException {
		ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
		try (ObjectInputStream objectInputStream = new ObjectInputStream(inputStream)) {
			return (T) objectInputStream.readObject();
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}
	}
}
